package com.hxkj.service.timer.impl;

import com.github.pagehelper.PageInfo;
import com.hxkj.client.service.ScheduleJobServiceClient;
import com.hxkj.domain.po.ScheduleJobEntity;
import com.hxkj.domain.vo.ScheduleJobVO;
import com.hxkj.service.timer.ScheduleJobService;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.result.ResultPageStruct;
import com.platform.common.domain.to.PageTo;
import com.platform.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
@Slf4j
public class ScheduleJobServiceImpl implements ScheduleJobService {

    private static final String regEx = "(((^([0-9]|[0-5][0-9])(\\,|\\-|\\/){1}([0-9]|[0-5][0-9]) )|^([0-9]|[0-5][0-9]) |^(\\* ))((([0-9]|[0-5][0-9])(\\,|\\-|\\/){1}([0-9]|[0-5][0-9]) )|([0-9]|[0-5][0-9]) |(\\* ))((([0-9]|[01][0-9]|2[0-3])(\\,|\\-|\\/){1}([0-9]|[01][0-9]|2[0-3]) )|([0-9]|[01][0-9]|2[0-3]) |(\\* ))((([0-9]|[0-2][0-9]|3[01])(\\,|\\-|\\/){1}([0-9]|[0-2][0-9]|3[01]) )|(([0-9]|[0-2][0-9]|3[01]) )|(\\? )|(\\* )|(([1-9]|[0-2][0-9]|3[01])L )|([1-7]W )|(LW )|([1-7]\\#[1-4] ))((([1-9]|0[1-9]|1[0-2])(\\,|\\-|\\/){1}([1-9]|0[1-9]|1[0-2]) )|([1-9]|0[1-9]|1[0-2]) |(\\* ))(([1-7](\\,|\\-|\\/){1}[1-7])|([1-7])|(\\?)|(\\*)|(([1-7]L)|([1-7]\\#[1-4]))))|(((^([0-9]|[0-5][0-9])(\\,|\\-|\\/){1}([0-9]|[0-5][0-9]) )|^([0-9]|[0-5][0-9]) |^(\\* ))((([0-9]|[0-5][0-9])(\\,|\\-|\\/){1}([0-9]|[0-5][0-9]) )|([0-9]|[0-5][0-9]) |(\\* ))((([0-9]|[01][0-9]|2[0-3])(\\,|\\-|\\/){1}([0-9]|[01][0-9]|2[0-3]) )|([0-9]|[01][0-9]|2[0-3]) |(\\* ))((([0-9]|[0-2][0-9]|3[01])(\\,|\\-|\\/){1}([0-9]|[0-2][0-9]|3[01]) )|(([0-9]|[0-2][0-9]|3[01]) )|(\\? )|(\\* )|(([1-9]|[0-2][0-9]|3[01])L )|([1-7]W )|(LW )|([1-7]\\#[1-4] ))((([1-9]|0[1-9]|1[0-2])(\\,|\\-|\\/){1}([1-9]|0[1-9]|1[0-2]) )|([1-9]|0[1-9]|1[0-2]) |(\\* ))(([1-7](\\,|\\-|\\/){1}[1-7] )|([1-7] )|(\\? )|(\\* )|(([1-7]L )|([1-7]\\#[1-4]) ))((19[789][0-9]|20[0-9][0-9])\\-(19[789][0-9]|20[0-9][0-9])))";

    @Autowired
    private ScheduleJobServiceClient jobServiceClient;

    @Override
    public ResultPageStruct<ScheduleJobVO> list(ScheduleJobEntity jobEntity, PageTo pageParam) throws Exception {
        if (pageParam.getPageNum() == null || pageParam.getPageSize() == null) {
            pageParam.setPageNum(1);
            pageParam.setPageSize(10);
        }
        ResultPageStruct<ScheduleJobVO> result = new ResultPageStruct<>();
        ParameterDataStruct<ScheduleJobEntity> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setData(jobEntity);
        parameterDataStruct.setPageTo(pageParam);

        result = jobServiceClient.list(parameterDataStruct);
        if(result.getPageInfo()==null){
            PageTo pageTo = new PageTo();
            result.setPageInfo(pageTo);
            throw new CustomMessageException("列表为空"+pageTo.getTotal());
        }
        /*try{

        }catch (Exception e){
            e.printStackTrace();
            log.error("获取定时任务列表失败:{}", e.getMessage());
        }*/
        return result;
    }

    @Override
    public ScheduleJobEntity info(Long jobId) throws Exception {
        if(jobId == null){
            throw new CustomMessageException("任务编号不能为空!");
        }
        ResultDataStruct<ScheduleJobEntity> result = jobServiceClient.info(jobId);
        if(result.getCode() != 200){
            throw new CustomMessageException(result.getCode(),result.getMessage());
        }
        return result.getData();
    }

    @Override
    public void save(ScheduleJobVO scheduleJob) throws Exception {
        if(scheduleJob == null){
            throw new CustomMessageException("定时任务为空！");
        }
        if(StringUtils.isBlank(scheduleJob.getBeanName())){
            throw new CustomMessageException("类名不能为空！");
        }
        if(StringUtils.isBlank(scheduleJob.getMethodName())){
            throw new CustomMessageException("方法名不能为空！");
        }
        if(StringUtils.isBlank(scheduleJob.getCronExpression())){
            throw new CustomMessageException("cron表达式不能为空！");
        }
        // 编译正则表达式
        Pattern pattern = Pattern.compile(regEx);
        Matcher matcher = pattern.matcher(scheduleJob.getCronExpression());
        // 字符串是否与正则表达式相匹配
        boolean flag = matcher.matches();
        if(!flag){
            throw new CustomMessageException("不符合规范的cron表达式！");
        }
        ScheduleJobEntity entity = new ScheduleJobEntity();
        entity.setStatus(scheduleJob.getStatus());
        entity.setBeanName(scheduleJob.getBeanName());
        entity.setCronExpression(scheduleJob.getCronExpression());
        entity.setMethodName(scheduleJob.getMethodName());
        entity.setParams(scheduleJob.getParams());
        entity.setRemark(scheduleJob.getRemark());
        ResultMessageStruct result = jobServiceClient.save(entity);
        if(result.getCode() != 200){
            throw new CustomMessageException(result.getCode(),result.getMessage());
        }
        //return result;
    }

    @Override
    public void update(ScheduleJobVO scheduleJob) throws Exception {
        if(scheduleJob == null){
            throw new CustomMessageException("定时任务为空！");
        }
        if(scheduleJob.getJobId()==null){
            throw new CustomMessageException("任务编号不能为空！");
        }
        if(StringUtils.isBlank(scheduleJob.getBeanName())){
            throw new CustomMessageException("类名不能为空！");
        }
        if(StringUtils.isBlank(scheduleJob.getMethodName())){
            throw new CustomMessageException("方法名不能为空！");
        }
        if(StringUtils.isBlank(scheduleJob.getCronExpression())){
            throw new CustomMessageException("cron表达式不能为空！");
        }
        /*boolean flag = regEx.matches(scheduleJob.getCronExpression());
        if(!flag){
            throw new CustomMessageException("不符合规范的cron表达式！");
        }*/
        ScheduleJobEntity entity = new ScheduleJobEntity();
        entity.setStatus(scheduleJob.getStatus());
        entity.setBeanName(scheduleJob.getBeanName());
        entity.setCronExpression(scheduleJob.getCronExpression());
        entity.setMethodName(scheduleJob.getMethodName());
        entity.setParams(scheduleJob.getParams());
        entity.setRemark(scheduleJob.getRemark());
        entity.setJobId(scheduleJob.getJobId());
        ResultMessageStruct result = jobServiceClient.update(entity);
        if(result.getCode() != 200){
            throw new CustomMessageException(result.getCode(),result.getMessage());
        }
        //return result;
    }

    @Override
    public void delete(String jobIds) throws Exception {
        if(StringUtils.isBlank(jobIds)){
            throw new CustomMessageException("任务编号不能为空!");
        }
        String[] ss = jobIds.split(",");
        Long[] ids = new Long[ss.length];
        for (int i = 0; i < ids.length; i++) {
            ids[i] = Long.valueOf(ss[i]);
        }
        ResultMessageStruct result = jobServiceClient.delete(ids);
        if(result.getCode() != 200){
            throw new CustomMessageException(result.getCode(),result.getMessage());
        }
        //return result;
    }

    @Override
    public void run(String jobIds) throws Exception {
        if(StringUtils.isBlank(jobIds)){
            throw new CustomMessageException("任务编号不能为空!");
        }
        String[] ss = jobIds.split(",");
        Long[] ids = new Long[ss.length];
        for (int i = 0; i < ids.length; i++) {
            ids[i] = Long.valueOf(ss[i]);
        }
        ResultMessageStruct result = jobServiceClient.run(ids);
        if(result.getCode() != 200){
            throw new CustomMessageException(result.getCode(),result.getMessage());
        }
        //return result;
    }

    @Override
    public void pause(String jobIds) throws Exception {
        if(StringUtils.isBlank(jobIds)){
            throw new CustomMessageException("任务编号不能为空!");
        }
        String[] ss = jobIds.split(",");
        Long[] ids = new Long[ss.length];
        for (int i = 0; i < ids.length; i++) {
            ids[i] = Long.valueOf(ss[i]);
        }
        ResultMessageStruct result = jobServiceClient.pause(ids);
        if(result.getCode() != 200){
            throw new CustomMessageException(result.getCode(),result.getMessage());
        }
        //return result;
    }

    @Override
    public void resume(String jobIds) throws Exception {
        if(StringUtils.isBlank(jobIds)){
            throw new CustomMessageException("任务编号不能为空!");
        }
        String[] ss = jobIds.split(",");
        Long[] ids = new Long[ss.length];
        for (int i = 0; i < ids.length; i++) {
            ids[i] = Long.valueOf(ss[i]);
        }
        ResultMessageStruct result = jobServiceClient.resume(ids);
        if(result.getCode() != 200){
            throw new CustomMessageException(result.getCode(),result.getMessage());
        }
        //return result;
    }

}
